//
//  OKSickDescVcTableViewController.m
//  ZLFastDoctor
//
//  Created by answer on 16/5/24.
//  Copyright © 2016年 LS. All rights reserved.
//

#import "OKSickDescVcTableViewController.h"
#import "OKSickComplicationModel.h"

static NSString *OKSickDescVcTableViewControllerIdentifier = @"OKSickDescVcTableViewControllerIdentifier";

@interface OKSickDescVcTableViewController ()
@property (strong, nonatomic) NSArray *data;

@property (strong, nonatomic) NSMutableArray *callBackArray;
@end

@implementation OKSickDescVcTableViewController
#pragma mark - set/get
- (void)setSickTags:(NSArray *)sickTags{
    _sickTags = sickTags;

}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:OKSickDescVcTableViewControllerIdentifier];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"确定" style:UIBarButtonItemStylePlain target:self action:@selector(sure)];
    
    
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    if (self.sickTags) {
        for (NSString *sick in self.sickTags) {
            NSInteger index = [self.data indexOfObject:sick];
            NSIndexPath *path = [NSIndexPath indexPathForItem:index inSection:0];
            UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:path];
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
        }
    }
}

#pragma mark - 导航栏右侧确定按钮点击响应事件
- (void)sure {
    for (NSString *sick in self.data) {
        NSInteger index = [self.data indexOfObject:sick];
        NSIndexPath *path = [NSIndexPath indexPathForItem:index inSection:0];
        UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:path];
        if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {
            [self.callBackArray addObject:sick];
        }
        
    }
    
    // 回调数据
    if ([self.delegate respondsToSelector:@selector(sickDescVcCallBack:)]) {
        [self.delegate sickDescVcCallBack:self.callBackArray.copy];
    }
    [self.navigationController popViewControllerAnimated:YES];
    
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.data.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:OKSickDescVcTableViewControllerIdentifier forIndexPath:indexPath];

    cell.textLabel.text = self.data[indexPath.row];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    //取出对应的cell
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    // 修改cell的样式
    (cell.accessoryType == UITableViewCellAccessoryCheckmark) ? (cell.accessoryType = UITableViewCellAccessoryNone) : (cell.accessoryType = UITableViewCellAccessoryCheckmark);
}

- (NSMutableArray *)callBackArray {
    if (_callBackArray == nil) {
        _callBackArray = [NSMutableArray array];
    }
    return _callBackArray;
}

- (NSArray *)data {
    if (_data == nil) {
        _data = @[@"腹痛", @"厌食", @"乏力", @"体重减轻", @"恶心", @"呕吐", @"呕血"];
    }
    return _data;
}

@end

